#include "stdafx.h"
#include "Marker.h"
#include "MyShape.h"
#include "BulletCollision\CollisionShapes\btBoxShape.h"

Marker::Marker(){
	_bHasShape = false;
	_vCol = btVector4(0.0f, 0.0f, 0.0f, 1.0f);
	_vPos = btVector3(20.0f, 0.0f, 12.5f);
	_pShp = NULL;
	_sOffset = 0.6f;
	_sOrientDeg = 0.0f;
}
Marker::~Marker(){

}
void Marker::setShape(MyShape* shp){
	_pShp = shp;
	btVector4 temp = _pShp->getColor();
	temp*=0.3f; // Set to a similiar color as the shape
	_vCol = temp;
	_eType = _pShp->type();
	_bHasShape = true;
}

void Marker::render(){
	//glLineWidth(4.0f);
	glColor4fv( _vCol );
	glLineWidth(6.0f);

	switch(_eType){
	case my_triangle:
		drawTri();
		break;
	case my_retangle:
		drawRect();
		break;
	case my_square:
		drawSquare();
		break;
	case my_cylinder:
		drawCylinder();
		break;
	};
}
void Marker::drawTri(){
	btScalar z = 12.5f;

	btScalar tempx = 12.0f/2.0f,
			 tempy = (cos(30.0f*PI/180.0f)*12.0f)/2.0f,
			 tempz = 4.0f/2.0f;

	glPushMatrix();
	glTranslatef(_vPos.x(), _vPos.y(), 12.5f);
	glRotatef(_sOrientDeg, 0.0f, 0.0f, 1.0f);
	glBegin(GL_LINE_LOOP);
		glVertex3f(-tempx - _sOffset,		-tempy - _sOffset,		z);
		glVertex3f(	tempx + _sOffset,		-tempy - _sOffset,		z);
		glVertex3f(	0.0f,					 tempy + _sOffset,		z);
	glEnd();
	glPopMatrix();
}
void Marker::drawRect(){
	btScalar z = 12.5f;

	const btBoxShape* boxShape = static_cast<const btBoxShape*>(_pShp->getCollisionShape());
	btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();

	glPushMatrix();
	glTranslatef(_vPos.x(), _vPos.y(), 12.5f);
	glRotatef(_sOrientDeg, 0.0f, 0.0f, 1.0f);
	glBegin(GL_LINE_LOOP);
		glVertex3f(-halfExtent.x() - _sOffset, -halfExtent.y() - _sOffset, z);
		glVertex3f(halfExtent.x() + _sOffset, -halfExtent.y() - _sOffset, z);
		glVertex3f(halfExtent.x() + _sOffset, halfExtent.y() + _sOffset, z);
		glVertex3f(-halfExtent.x() - _sOffset, halfExtent.y() + _sOffset, z);
		//glVertex3f(vMin.x(), vMin.y(), z);
	glEnd();
	glPopMatrix();
}
void Marker::drawSquare(){
	btScalar z = 12.5f;

	const btBoxShape* boxShape = static_cast<const btBoxShape*>(_pShp->getCollisionShape());
	btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();

	glPushMatrix();
	glTranslatef(_vPos.x(), _vPos.y(), 12.5f);
	glRotatef(_sOrientDeg, 0.0f, 0.0f, 1.0f);
	glBegin(GL_LINE_LOOP);
		glVertex3f(-halfExtent.x() - _sOffset, -halfExtent.y() - _sOffset, z);
		glVertex3f(halfExtent.x() + _sOffset, -halfExtent.y() - _sOffset, z);
		glVertex3f(halfExtent.x() + _sOffset, halfExtent.y() + _sOffset, z);
		glVertex3f(-halfExtent.x() - _sOffset, halfExtent.y() + _sOffset, z);
		//glVertex3f(vMin.x(), vMin.y(), z);
	glEnd();
	glPopMatrix();
}
void Marker::drawCylinder(){
	btScalar z = 12.5f;

	const btBoxShape* boxShape = static_cast<const btBoxShape*>(_pShp->getCollisionShape());
	btVector3 halfExtent = boxShape->getHalfExtentsWithMargin();

	//btVector3 vMin, vMax;
	//_pShp->getAabb(vMin, vMax);
	float radius     = halfExtent[0] + _sOffset;
	glPushMatrix();
	glTranslatef(_vPos.x(), _vPos.y(), 12.5f);
	glRotatef(_sOrientDeg, 0.0f, 0.0f, 1.0f);
	glBegin(GL_LINE_LOOP);
		for(int d = 0; d < 360; d++){
			glVertex3f(radius*cos(d*PI/180.0f), radius*sin(d*PI/180.0f), z);
		}
	glEnd();
	glPopMatrix();
}